<% if ((local_assigns[:commentable_type] == 'moment' || local_assigns[:commentable_type] == 'strategy') && local_assigns[:data].comment && (current_user.id == local_assigns[:data].userid || local_assigns[:no_hide_page])) ||
  (local_assigns[:commentable_type] == 'meeting' && local_assigns[:is_member]) %>

  <div class="horizonal_divider"></div>

  <div class="small_margin_top">
  <%= form_for local_assigns[:comment], :url => { :action => "comment" }, :html => { :method => "post" } do |f| %>
    <% if local_assigns[:comment].errors.any? %>
        <div class="error_explanation">
          <%= t('common.form.error_explanation') %>
        </div>
    <% end %>

    <div class="field no_margin_bottom">
      <%= f.text_area :comment, class: 'comment_textarea' %>
      <%= render :partial => '/shared/character_count', locals: { data: 'comment_comment' } %>
    </div>

    <% if local_assigns[:comments].length == 0 %>
    <div class="actions align_right no_margin_bottom">
    <% else %>
    <div class="actions align_right">
    <% end %>
      <%= f.hidden_field :commentable_type, :value => local_assigns[:commentable_type] %>
      <%= f.hidden_field :comment_by, :value => current_user.id %>
      <%= f.hidden_field :commentable_id, :value => local_assigns[:data].id %>
      <% if (local_assigns[:commentable_type] == 'moment' || local_assigns[:commentable_type] == 'strategy') && local_assigns[:data].userid != current_user.id %>
        <%= f.select :visibility, [
          [t('shared.comments.share_everyone'), 'all'],
          [t('shared.comments.share_with', name: User.where(:id => local_assigns[:data].userid).first.name), 'private']
        ] %>
      <% else %>
        <%= f.hidden_field :visibility, :value => 'all' %>
        <% if (local_assigns[:commentable_type] == 'moment' || local_assigns[:commentable_type] == 'strategy') && !local_assigns[:data].viewers.blank? && local_assigns[:data].viewers.length > 0 %>
         <%= f.select :viewers, local_assigns[:data].viewers.collect { |v|
              [ t('shared.comments.share_with', name: User.where(id: v).first.name), v ]
             }, include_blank: t('shared.comments.share_everyone') %>
        <% end %>
      <% end %>
      <%= f.submit t('comment.singular'), class: 'no_margin_right small_margin_top', id: 'add_comment_button' %>
    </div>

    <div class="clear"></div>
  <% end %>

  <div id="comments">
  <% local_assigns[:comments].each do |comment| %>
    <% if comment.visibility == 'all' ||
      (comment.visibility == 'private' && User.where(id: comment.comment_by).exists? &&
        (comment.comment_by == current_user.id ||
        local_assigns[:is_member] ||
        current_user.id == local_assigns[:data].userid ||
        (!comment.viewers.blank? && comment.viewers.include?(current_user.id))
        )) %>
        <% result = generate_comment(comment, comment.commentable_type) %>
        <% if local_assigns[:comments].first == comment %>
        <div class="comment no_margin_top" id=<%= 'comment_' + comment.id.to_s %>>
        <% else %>
        <div class="comment" id=<%= 'comment_' + comment.id.to_s %>>
        <% end %>
          <div class="table">
            <div class="table_row">
              <div class="table_cell small_profile_picture_div vertical_align_middle padding_right">
                <%= raw result[:profile_picture] %>
              </div>
              <div class="table_cell">
                <div class="comment_info">
                  <%= raw result[:comment_info] %>
                </div>
                <div class="comment_text">
                  <%= raw result[:comment_text] %>
                </div>
                <div class="subtle">
                  <%= raw result[:visibility] %>
                </div>
              </div>
              <%= raw result[:delete_comment] %>
            </div>
          </div>
        </div>
      <% end %>
  <% end %>
  </div>
  </div>
<% end %>
